<template>
  <el-form ref="form"  :rules="formRules" :model="news" label-width="80px" label-position="left">
    <el-form-item label="新闻标题" prop="title">
      <el-input type="text" v-model="news.title" ></el-input>
    </el-form-item>
    <el-form-item label="新闻内容" prop="content">
      <el-input type="text" v-model="news.content"></el-input>
    </el-form-item>
    <el-form-item label="作者" prop="author">
      <el-input type="text" v-model="news.author"></el-input>
    </el-form-item>
    <el-form-item label="发表时间">
      <el-date-picker v-model="news.createDate" type="datetime" value-format="yyyy-MM-dd"></el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="warning" @click="insertNews()">添加</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
    export default {
      name: "InsertNews",
      data(){
        let checkAuthor=(rule,value,callback)=>{
          console.log(value)
          if(value==null){
            callback(new Error("作者不能为空"))
          }else{
            callback()
          }
        }
          return{
           news:{},
            formRules:{
              title:[{required:true,message:"标题不能为空",trigger:"blur"}],
              content:[{required:true,message:"内容不能为空",trigger:"blur"}],
              author:[{ validator:checkAuthor,trigger:"blur"}]
            }
          }
      },
      methods:{
        insertNews(){
          this.$refs["form"].validate((va)=>{
            if(va){
              this.axios({
                method:"post",
                url:"/news",
                data:this.news
              }).then((res)=>{
                alert("添加成功")
                this.$router.push("/showNews")
              })
            }
            return va;
          })

        }
      }
    }
</script>

<style scoped>

</style>
